/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp;

import java.io.IOException;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import dk.redia.lib.utils.AppApplication;
import dk.redia.libraryapp.utils.LibraryItemizedOverlay;

public class LibraryMapActivity extends MapActivity {

	private static final String logTag = "LibraryMapActivity";
	public static final String EXTRA_KEY_ADDRESS = "address";
	public static final String EXTRA_KEY_TITLE = "title";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.librarymap);
	    String address = getIntent().getExtras().getString(LibraryMapActivity.EXTRA_KEY_ADDRESS);
	    String title = getIntent().getExtras().getString(LibraryMapActivity.EXTRA_KEY_TITLE);
	    if (address==null || address.length()==0) {
	    	Log.d(logTag, "No address provided");
	    	AppApplication.toast(R.string.libraryAddressCouldNotBePlottedOnMapText, Toast.LENGTH_LONG);
	    	finish();
	    	return;
	    }
	    if (title==null || title.length()==0) {
	    	title = "Biblioteket";
	    }
	    Log.d(logTag, "Looking for address " + address);
	    MapView mv = (MapView)findViewById(R.id.mapActivityMapView);
	    try {
			List<Address> results = new Geocoder(this).getFromLocationName(address, 1);
			if (results.size()>0) {
				
				List<Overlay> mapOverlays = mv.getOverlays();
				Drawable drawable = this.getResources().getDrawable(R.drawable.mappin);
				LibraryItemizedOverlay itemizedOverlay = new LibraryItemizedOverlay(drawable);
				
				double lat = results.get(0).getLatitude();
				double lon = results.get(0).getLongitude();				
				GeoPoint g = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
				OverlayItem overlayitem = new OverlayItem(g, title, "");
				itemizedOverlay.addOverlay(overlayitem);
				mapOverlays.add(itemizedOverlay);
				mv.setSatellite(true);
				mv.getController().setZoom(18);
				mv.getController().animateTo(g);
			} else {
				Log.d(logTag, "Failed to find location for address");
				AppApplication.toast(R.string.libraryAddressCouldNotBePlottedOnMapText, Toast.LENGTH_LONG);
				finish();
				return;
			}
		} catch (IOException e) {
			e.printStackTrace();
			AppApplication.toast(R.string.noNetworkText, Toast.LENGTH_LONG);
			finish();
			return;
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
